import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Welcome from '../components/Welcome.vue'
import Home from '../views/Home.vue'
import Users from '../components/User/Users.vue'
import Power from '../components/Power/PowerList.vue'
import Roles from '../components/Power/Roles.vue'
import  Cate from '../components/goods/Cate.vue'
import  Params from '../components/goods/Params.vue'
import  GoodsList from '../components/goods/GoodsList.vue'
import  Goodsadd from '../components/goods/Goodsadd.vue'
Vue.use(VueRouter)

  const routes = [
    {
      path: '/',
      redirect: '/login'
    },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect:'/welcome',
    children:[
      {
        path: '/welcome',
        name: 'Welcome',
        component: Welcome
      },
      {
        path: '/users',
        name: 'Users',
        component: Users
      },
      {
        path: '/rights',
        name: 'rights',
        component: Power
      },
      {
        path: '/roles',
        name: 'Roles',
        component: Roles
      },
      {
        path: '/categories',
        name: 'Cate',
        component: Cate
      },
      {
        path: '/params',
        name: 'Params',
        component: Params
      },
      {
        path: '/goods',
        name: 'GoodsList',
        component: GoodsList
      },
      {
        path: '/goods/add',
        name: 'Goodsadd',
        component: Goodsadd
      },
    ]
  }
]

const router = new VueRouter({
  routes
})
  // 导航守卫
router.beforeEach((to,from,next) =>{
   if (to.path =='/login') {
     return next()
   }
  var resp= window.sessionStorage.getItem('token');
   if(!resp) {
     return next('/login')
   }
   next()

})

export default router
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
